package com.lyrcsoft;

import com.lyrcsoft.conf.AppConfig;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.File;

/**
 * @author fujc-dev@qq.com
 * @date 2023/7/12 17:13
 */

public class Programs {

    public static void main(String[] args) throws LifecycleException {
        File base = new File(System.getProperty("java.io.tmpdir"));
        //
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.addContext("/", base.getAbsolutePath());
        tomcat.start();
        //
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);
        context.refresh();
        //创建 servlet 并添加到 tomcat 中
        DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
        Wrapper wrapper = tomcat.addServlet("/", "app", dispatcherServlet);
        wrapper.setLoadOnStartup(1);
        wrapper.addMapping("/");
        tomcat.getServer().await();
    }
}
